﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using ANGFLib;

namespace ANGF
{
    public partial class FormGeneralHtml : Form
    {
        private string htmlForm;
        private Dictionary<string, string> result = new Dictionary<string,string>();
        public FormGeneralHtml(string title, string htmlForm)
        {
            this.htmlForm = "<html><head><meta http-equiv='X-UA-Compatible' content='IE=edge'> <script>"
            + "\r\n"
            + General.LoadEmbededResourceAsText(Assembly.GetExecutingAssembly(), "ANGF.Resources.jquery.js")
            + "\r\n"
                // JQMを読み込ませるとなぜかスクリプトエラーになる
                //+ General.LoadEmbededResourceAsText(Assembly.GetExecutingAssembly(), "ANGF.Resources.jquery.mobile.js")
            + "\r\n"
            + "</script>"
            + "<style type='text/css'>"
            + "\r\n"
                // JQMを読み込ませるとなぜかスクリプトエラーになる
                //+ General.LoadEmbededResourceAsText(Assembly.GetExecutingAssembly(), "ANGF.Resources.jquery.mobile.css")
            + "\r\n"
            + "</style></head><body><div style='margin: 2em;'>"
            + htmlForm
            + "</div></body></html>";
            InitializeComponent();
            //webBrowser1.ScriptErrorsSuppressed = true;
            this.Text = title;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            result.Add("OK", "true");
            foreach (HtmlElement item in webBrowser1.Document.All)
            {
                if (string.IsNullOrWhiteSpace(item.Id)) continue;
                string val = "";
                if (item.TagName == "INPUT" && item.GetAttribute("type").ToLower() == "checkbox" && item.GetAttribute("checked") == "True") val = "True";
                else if (item.TagName == "INPUT" && item.GetAttribute("type").ToLower() == "radio" && item.GetAttribute("checked") == "True") val = "True";
                else if (item.TagName == "OPTION" && item.GetAttribute("selected") == "True") val = "True";
                else if (item.TagName == "INPUT" && item.GetAttribute("type").ToLower() == "text") val = item.GetAttribute("value");
                else if (item.TagName == "TEXTAREA") val = item.InnerText;
                else continue;
                result.Add("htmlform_" + item.Id, val);
            }
            Close();
        }

        private void FormGeneralHtml_Load(object sender, EventArgs e)
        {
            webBrowser1.DocumentText = htmlForm;
        }

        internal Dictionary<string, string> GetResult()
        {
            return result;
        }
    }
}
